package com.qs.eggs.view.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.MainActivity;
import com.qs.eggs.R;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.bean.SimpleBackPage;
import com.qs.eggs.refresh.utils.TDevice;
import com.qs.eggs.user.UserInfoActivity;
import com.qs.eggs.utils.UIHelper;
import com.qs.eggs.view.activity.AddressManagementActivity;
import com.qs.eggs.view.activity.CollectActivity;
import com.qs.eggs.view.activity.FootPrintActivity;
import com.qs.eggs.view.ordermanager.activity.OrderManagerActivity;
import com.qs.eggs.view.activity.RechargeRecordActivity;
import com.qs.eggs.view.ordermanager.activity.WaitEvaluationActivity;
import com.qs.eggs.view.ordermanager.activity.WaitGetActivity;
import com.qs.eggs.view.ordermanager.activity.WaitPayActivity;
import com.qs.eggs.view.ordermanager.activity.WaitSendActivity;
import com.qs.eggs.view.recharge.activity.RechargeActivity;
import com.qs.eggs.widgets.custom.PullToRefreshView;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;

import okhttp3.Call;
import okhttp3.Response;

public class MineFragment extends BaseFragment implements OnClickListener,PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener{
    private PullToRefreshView mPullToRefreshView;
    private TextView allOrder;
    public static TextView custname,yue;
    public static SimpleDraweeView mAvator;
    private LinearLayout waitPay,mll2,mll3,mll4,rl1;

    private TextView mCollect,zuji,address,chongzhijilu,kefu;
    public static String xingming,imageUrl,balance;
    private RelativeLayout chongzhi;
    public static MineFragment instance;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;

    }
    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        instance=this;

        mAvator=findView(R.id.profile_image);
        custname=findView(R.id.custname);
        rl1=findView(R.id.rl1);
        yue=findView(R.id.tv_chongzhi);
        chongzhi=findView(R.id.rl_chongzhi);
        allOrder=findView(R.id.tv_order_all);
        waitPay=findView(R.id.lly_waitpay);
        mCollect=findView(R.id.tv_collect);
        zuji=findView(R.id.tv_zuji);
        address=findView(R.id.tv_address);
        chongzhijilu=findView(R.id.tv_chongzhijilu);
        kefu=findView(R.id.tv_kefu);
        mPullToRefreshView =findView(R.id.main_pull_refresh_view);

        mll2=findView(R.id.ll2);
        mll3=findView(R.id.ll3);
        mll4=findView(R.id.ll4);

        mAvator.setOnClickListener(this);
        allOrder.setOnClickListener(this);
        waitPay.setOnClickListener(this);
        mll2.setOnClickListener(this);
        mll3.setOnClickListener(this);
        mll4.setOnClickListener(this);
        mCollect.setOnClickListener(this);
        zuji.setOnClickListener(this);
        address.setOnClickListener(this);
        chongzhijilu.setOnClickListener(this);
        kefu.setOnClickListener(this);
        rl1.setOnClickListener(this);
        chongzhi.setOnClickListener(this);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.rl1:
                if(!TDevice.hasInternet()){
                    Toast.makeText(getContext(),"网络未连接",Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(getContext(),UserInfoActivity.class));
                break;
            case R.id.tv_order_all:
                startActivity(new Intent(getContext(),OrderManagerActivity.class));
                break;
            case R.id.lly_waitpay:
                startActivity(new Intent(getContext(), WaitPayActivity.class));
                break;
            case R.id.ll2:
                startActivity(new Intent(getContext(), WaitSendActivity.class));

                break;
            case R.id.ll3:
                startActivity(new Intent(getContext(), WaitGetActivity.class));

                break;
            case R.id.ll4:
                startActivity(new Intent(getContext(), WaitEvaluationActivity.class));

                break;
            case R.id.rl_chongzhi:
                startActivity(new Intent(getContext(),RechargeActivity.class));
                break;
            case R.id.tv_collect:
                startActivity(new Intent(getContext(), CollectActivity.class));
                break;
            case R.id.tv_zuji:
                startActivity(new Intent(getContext(), FootPrintActivity.class));
                break;
            case R.id.tv_chongzhijilu:
                startActivity(new Intent(getContext(), RechargeRecordActivity.class));
                break;
            case R.id.tv_address:
                startActivity(new Intent(getContext(), AddressManagementActivity.class));
                break;
            case R.id.tv_kefu:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:13561753715"));

                startActivity(intent);
                break;
        }
    }

    //获取个人信息
    public static void getInfo(){
        OkGo.post(Constants.URL_BASE+"personal?action=getPersonalInfo")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("个人信息=="+s);
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                xingming=jsonArray.getJSONObject(i).getString("name");
                                balance=jsonArray.getJSONObject(i).getString("balance");
                                imageUrl= Constants.URL_BASE+jsonArray.getJSONObject(i).getString("folder")+"/"
                                        +jsonArray.getJSONObject(i).getString("autoname");
                                mAvator.setImageURI(imageUrl);
                                custname.setText(xingming);
                                yue.setText("账户余额: "+"¥"+balance);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                getInfo();
                mPullToRefreshView.onFooterRefreshComplete();
            }
        }, 1000);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                getInfo();
                mPullToRefreshView.onHeaderRefreshComplete();
            }
        }, 1000);

    }
	/*-----------刷新接口方法实现-------	*/

    Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            mPullToRefreshView.onFooterRefreshComplete();

        }

        ;
    };

    @Override
    public void onResume(){
        super.onResume();
        //获取个人信息
        if(!TDevice.hasInternet()){
            Toast.makeText(getContext(),"网络未连接",Toast.LENGTH_SHORT).show();
        }else {
            HttpApi.cheakIsLogin(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                   if(s.equals("\"true\"")){
                       getInfo();
                   }else {
                       MainActivity.mNavBar.select(0);
                   }
                }
            });

        }
    }
}
